import operator
import random
import defaultvalue
import randommove
import astar
import searchalg

##################################################
# search for a route that moves to the destination with severla random turns
##################################################
class TurnsNpc(searchalg.SearchAlg):
    def __init__(self):
        self.map = []
        self.way = []
        self.turn_pos1 = [] # the first turn position
        self.turn_pos2 = [] # the second turn position
        self.s_pos = [-1, -1]
        self.status = 0 # the start state of the FSM
        self.reset_time = 10

    def produce_turns(self):
        """ produce turns when search route begins """
        if len(self.turn_pos1) == 0: 
            while 1:
                self.turn_pos1 = []                
                self.turn_pos1.append(random.randint(0, self.length - 1))
                self.turn_pos1.append(random.randint(0, self.height - 1))
                if self.map[self.turn_pos1[0]][self.turn_pos1[1]] == 0: # no blocks
                    break
                
        if len(self.turn_pos2) == 0 :
            while 1:
                self.turn_pos2 = []
                self.turn_pos2.append(random.randint(0, self.length - 1))
                self.turn_pos2.append(random.randint(0, self.height - 1))
                if self.map[self.turn_pos2[0]][self.turn_pos2[1]] == 0 : # no blocks
                    break

    def adjust_turns(self, start_position):
        """ adjust turn points when the tanker is walled up """
        if self.s_pos == start_position: # the tanker is walled up
            if self.status == 0: # the first state: tanker moves the the first turn
                while 1 :
                    i = random.randint(0, self.length - 1)
                    j = random.randint(0, self.height - 1)
                    if self.map[i][j] == 0:
                        break
                self.turn_pos1 = [i , j]
            if self.status == 1: # the second state : tanker moves the second turn
                while 1 :
                    i = random.randint(0, self.length - 1)
                    j = random.randint(0, self.height - 1)
                    if self.map[i][j] == 0:
                        break
                self.turn_pos2 = [i , j]        
        
    def search(self, map, start_position, end_position):
        """ search for the route """
        self.map = map
        self.length = len(map)
        self.height = len(map[0])

        self.produce_turns() # judge if the turns have been setted
        self.adjust_turns(start_position) # judge if the tanker has been walled up
                
        self.s_pos = start_position
        self.destination = end_position

        search_al = astar.AStar() # the A star seach algorithm
        if self.status == 0: # the first state, run to the first turn
            self.reset_time -= 1
            if self.reset_time <= 0 :
                self.reset_time = 10
                self.status = 1
            if self.s_pos == self.turn_pos1: # reach, go to the second turn
                self.status = 1
            else:
                self.way = search_al.search(self.map, self.s_pos, self.turn_pos1)
        if self.status == 1: # the second state, run to the second turn
            self.reset_time -= 1
            if self.reset_time <= 0 :
                self.reset_time = 10
                self.status = 2
            if self.s_pos == self.turn_pos2:
                self.status = 2
            else:
                self.way = search_al.search(self.map, self.s_pos, self.turn_pos2)
        if self.status == 2: # finally, run to the distination
            self.way = search_al.search(self.map, self.s_pos, self.destination)
        return self.way
   